package com.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.commons.dbutils.DbUtils;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * 新添加了数据库连接池技术
 * @auther user
 * @date 2021/12/6 - 3:04
 * @Description JDBCUtils
 */
public class JDBCUtils {
    /**
     * @Description 获取数据库连接
     * @return
     * @throws Exception
     */
    public static Connection getConnection() throws Exception {
        //1 读取配置文件
        InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(inputStream);
        //获取四个基本信息
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driverClass = properties.getProperty("driverClass");
        //加载驱动
        Class.forName(driverClass);
        //获取连接
        Connection connection = DriverManager.getConnection(url, user, password);
        return connection;
    }
    /**
     * 使用C3P0数据库连接池技术
     * @return
     * @throws Exception
     */
    //数据库连接池只需提供一个，所以设为全局变量
    private static ComboPooledDataSource cpds = new ComboPooledDataSource("C3P0Config");
    public static Connection getConnection1() throws Exception{
        Connection connection = cpds.getConnection();
        return connection;
    }

    /**
     * 使用Dbcp数据库连接池技术
     * @return
     * @throws Exception
     */
    private static DataSource dataSource = null;
    static {
        try {
            //加载配置文件
            Properties properties = new Properties();
            //方式一；
            InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("dbcp.properties");
            //方式二
            //FileInputStream inputStream = new FileInputStream(new File("src/dbcp.properties"));
            properties.load(inputStream);
            dataSource = BasicDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection2() throws Exception{
        Connection connection = dataSource.getConnection();
        return connection;
    }

    /**
     * 使用Druid德鲁伊数据库连接池技术
     * @return
     * @throws Exception
     */
    private static DataSource ds = null;
    static {
        try {
            //加载配置文件
            Properties properties = new Properties();
            InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
            properties.load(inputStream);
            //创建数据库连接池
            ds = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection3() throws Exception{
        Connection connection = ds.getConnection();
        return connection;
    }

    /**
     * @Description 关闭Connection连接和Statement
     * @param connection
     * @param statement
     */
    public static void closeResource(Connection connection, Statement statement){
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * @Description 关闭Connection连接和Statement和ResultSet
     * @param connection
     * @param statement
     */
    public static void closeResource(Connection connection, Statement statement, ResultSet resultSet){
        try {
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 使用DBUtils工具类，实现资源的关闭
     * @param connection
     * @param statement
     * @param resultSet
     */
    public static void closeResource1(Connection connection, Statement statement, ResultSet resultSet){
/*        try {
            DbUtils.close(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            DbUtils.close(statement);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            DbUtils.close(resultSet);
        } catch (SQLException e) {
            e.printStackTrace();
        }*/
        //悄悄地关
        DbUtils.closeQuietly(connection);
        DbUtils.closeQuietly(connection);
        DbUtils.closeQuietly(connection);
    }
}
